package com.sgcc.pda.mdrh.zoom.plugin;

import android.content.Context;

import com.qihoo360.replugin.RePlugin;
import com.sgcc.pda.baselibrary.utils.LogUtil;
import com.sgcc.pda.greendao.entity.AppBaseInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 基础服务插件相关配置
 *
 * @author Ben
 * @version 2019.08.29
 */
public class InnerPluginUtil {
    /**
     * 硬件服务(HW) - 包名, APP Name, 资源文件地址, 插件别名
     */
    private final static String APP_PKG_HW = "com.sgcc.pda.hw";
    private final static String APP_NAME_HW = "硬件服务";
    private final static String APP_ASSETS_HW = "apk/hw.apk";
    private final static String APP_ALIAS_HW = "hw";

    /**
     * GIS服务(原生) - 包名, APP Name, 资源文件地址, 插件别名
     */
    private final static String APP_PKG_GIS = "com.sgcc.pda.gis";
    private final static String APP_NAME_GIS = "GIS服务";
    private final static String APP_ASSETS_GIS = "apk/gis.apk";
    private final static String APP_ALIAS_GIS = "gis";

    /**
     * 北京GIS门户app
     */
    private final static String APP_PKG_GIS_FACE = "com.sgcc.pda.mdrh.uniongisface";
    private final static String APP_NAME_GIS_FACE = "GIS门户";
    private final static String APP_ASSETS_GIS_FACE = "apk/gisface.apk";
    private final static String APP_ALIAS_GIS_FACE = "gisface";
    /**
     * 安全管控服务
     */
    private final static String APP_PKG_SAFE = "com.sgcc.pda.mdrh.task.safe";
    private final static String APP_NAME_SAFE = "安全管控服务";
    private final static String APP_ASSETS_SAFE = "apk/safe.apk";
    private final static String APP_ALIAS_SAFE = "safe";

    /**
     * 人脸识别服务
     */
    private final static String APP_PKG_FACE = "com.sgcc.pda.safe.faceplugin";
    private final static String APP_NAME_FACE = "人脸识别";
    private final static String APP_ASSETS_FACE = "apk/safe-face.apk";
    public final static String APP_ALIAS_FACE = "safeFace";

    /**
     * 基础服务版本号验证
     */
    private static final int VERSION_HW = 39;
    private static final int VERSION_SAFE = 40;
    private static final int VERSION_SAFE_FACE = 17;

    private static final HashMap<String, Integer> PLUGIN_VERSION = new HashMap<>();

    static {
        // 插件别名 /gis包名
        PLUGIN_VERSION.put(APP_ALIAS_FACE, VERSION_SAFE_FACE);
//        PLUGIN_VERSION.put(APP_PKG_SAFE, VERSION_SAFE);
    }


    /**
     * 获取未安装的基础服务列表
     * <p>
     * 只返回未正确安装的必装基础服务
     * 如果为空 则表示已经安装完成所有必转应用
     *
     * @return List<AppBaseInfo>
     */
    public static List<AppBaseInfo> getUninstallList(Context context) {
        boolean isInstall;
        List<AppBaseInfo> uninstallList = new ArrayList<>();
        List<AppBaseInfo> list = getList();
        for (AppBaseInfo item : list) {
            isInstall = isInstalled(item.getAppAlias());
            //没有安装或者安装的版本号小于宿主携带的插件版本号
            if (!isInstall) {
                item.setIsInstalled(Const.AppState.APP_STATES_NO_INSTALL);
                uninstallList.add(item);
            }
        }
        return uninstallList;
    }

    /**
     * 获取基础服务应用列表(基础服务是必须安装的插件)
     *
     * @return app_package, app_name, assets_path, app_alias
     */
    private static List<AppBaseInfo> getList() {
        List<AppBaseInfo> list = new ArrayList<>();
        // 基础服务包名
        String[] packages = {APP_PKG_FACE};
        // 基础服务名称
        String[] names = {APP_NAME_FACE};
        // 基础服务 assets path
        String[] assets = {APP_ASSETS_FACE};
        // 基础服务插件别名
        String[] alias = {APP_ALIAS_FACE};
        // 取包名集合长度
        int len = packages.length;
        for (int i = 0; i < len; i++) {
            AppBaseInfo info = new AppBaseInfo();
            info.setAppPackage(packages[i]);
            info.setAppName(names[i]);
            info.setAssetsPath(assets[i]);
            info.setAppAlias(alias[i]);
            list.add(info);
        }
        return list;
    }

    /**
     * 判断基础服务是否已经正确安装(已安装与集合中一致的版本)
     *
     * @param alias 插件名
     * @return true已安装正确版本  false未安装正确的版本
     */
    private static boolean isInstalled(String alias) {
        // 已安装插件版本
        long pluginVersion = RePlugin.getPluginVersion(alias);
        LogUtil.e("cj", alias + "插件版本 " + pluginVersion);
        // 内置版本
        int baseVersion = PLUGIN_VERSION.get(alias) != null ? PLUGIN_VERSION.get(alias) : 0;
        return pluginVersion >= baseVersion;
    }
}
